package com.example.p_ttcheng.rrk.activities;

import android.annotation.TargetApi;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;

import com.example.p_ttcheng.rrk.R;
import com.example.p_ttcheng.rrk.fragements.MyFragment;
import com.example.p_ttcheng.rrk.fragements.RentFragment;
import com.example.p_ttcheng.rrk.fragements.SellFragement;
import com.example.p_ttcheng.rrk.fragements.WalletFragment;
import com.example.p_ttcheng.rrk.fragements.ZiChanFragment;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private FrameLayout mContent;
    private RentFragment mRentFragment;     //租用界面，首页面
    private SellFragement mSellFragment;    //出售界面，次页面
    private ZiChanFragment mZiChanFragment; //资产界面，中界面
    private WalletFragment mWalletFragment;
    private MyFragment mMyFragment;
    private FragmentManager mFragmentManager;

    private Button mBtnSell, mBtnRent, mBtnZichan, mBtnWallet, mBtnMe;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mContent = (FrameLayout) findViewById(R.id.content);
        mBtnRent = (Button) findViewById(R.id.btn_rent);
        mBtnZichan = (Button) findViewById(R.id.btn_zichan);
        mBtnWallet = (Button) findViewById(R.id.btn_wallet);
        mBtnMe = (Button) findViewById(R.id.btn_me);
        mBtnSell = (Button) findViewById(R.id.btn_sell);
        mBtnRent.setOnClickListener(this);
        mBtnZichan.setOnClickListener(this);
        mBtnWallet.setOnClickListener(this);
        mBtnSell.setOnClickListener(this);
        mBtnMe.setOnClickListener(this);

        mFragmentManager = getSupportFragmentManager();
        mSellFragment = new SellFragement();
        mRentFragment = new RentFragment();
        mZiChanFragment = new ZiChanFragment();
        mWalletFragment = new WalletFragment();
        mMyFragment = new MyFragment();
        mFragmentManager.beginTransaction().add(R.id.content, mRentFragment).commit();

    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_rent:
                mFragmentManager.beginTransaction().replace(R.id.content, mRentFragment).commit();
                v.setBackground(getDrawable(R.mipmap.main_rent_click));
                mBtnSell.setBackground(getDrawable(R.mipmap.main_sell_unclick));
                mBtnMe.setBackground(getDrawable(R.mipmap.main_me_unclick));
                mBtnZichan.setBackground(getDrawable(R.mipmap.main_assets_unclick));
                mBtnWallet.setBackground(getDrawable(R.mipmap.main_wallet_unclick));
                break;
            case R.id.btn_sell:
                mFragmentManager.beginTransaction().replace(R.id.content, mSellFragment).commit();
                v.setBackground(getDrawable(R.mipmap.main_sell_click));
                mBtnRent.setBackground(getDrawable(R.mipmap.main_rent_unclick));
                mBtnMe.setBackground(getDrawable(R.mipmap.main_me_unclick));
                mBtnZichan.setBackground(getDrawable(R.mipmap.main_assets_unclick));
                mBtnWallet.setBackground(getDrawable(R.mipmap.main_wallet_unclick));
                break;
            case R.id.btn_zichan:
                mFragmentManager.beginTransaction().replace(R.id.content, mZiChanFragment).commit();
                v.setBackground(getDrawable(R.mipmap.main_assets_click));
                mBtnRent.setBackground(getDrawable(R.mipmap.main_rent_unclick));
                mBtnMe.setBackground(getDrawable(R.mipmap.main_me_unclick));
                mBtnSell.setBackground(getDrawable(R.mipmap.main_sell_unclick));
                mBtnWallet.setBackground(getDrawable(R.mipmap.main_wallet_unclick));
                break;
            case R.id.btn_wallet:
                mFragmentManager.beginTransaction().replace(R.id.content, mWalletFragment).commit();
                v.setBackground(getDrawable(R.mipmap.main_wallet_click));
                mBtnRent.setBackground(getDrawable(R.mipmap.main_rent_unclick));
                mBtnMe.setBackground(getDrawable(R.mipmap.main_me_unclick));
                mBtnSell.setBackground(getDrawable(R.mipmap.main_sell_unclick));
                mBtnZichan.setBackground(getDrawable(R.mipmap.main_assets_unclick));
                break;
            case R.id.btn_me:
                mFragmentManager.beginTransaction().replace(R.id.content, mMyFragment).commit();
                v.setBackground(getDrawable(R.mipmap.main_me_click));
                mBtnRent.setBackground(getDrawable(R.mipmap.main_rent_unclick));
                mBtnZichan.setBackground(getDrawable(R.mipmap.main_assets_unclick));
                mBtnSell.setBackground(getDrawable(R.mipmap.main_sell_unclick));
                mBtnWallet.setBackground(getDrawable(R.mipmap.main_wallet_unclick));
                break;

        }
    }


}
